const { UserModel } = require("../model/user")

module.exports = class UserService {
    async findAll() {
        return await UserModel.findAll()
    }
    async findOneByUsername(username) {
        return await UserModel.findOne({
            username,
        })
    }
    async findOneById(_id) {
        return await UserModel.findOne({
            _id,
        })
    }
    async create(item) {
        const model = new UserModel({
            ...item
        })
        await model.save()
    }
    async isExistByUserName(username) {
        const item = await UserModel.findOne({
            username,
        })
        if (item) {
            return true
        }
        return false
    }
    async createRepoItemByUsername(username, repoItem) {
        const doc = await this.findOneByUsername(username)
        if (doc) {
            doc.repos.push(repoItem)
            const model = new UserModel(doc)
            await model.save()
        }
    }
}